package com.hjqjl.whdemo.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hjqjl.whdemo.R;
import com.hjqjl.whdemo.utils.StringUtils;

public class TitleLayout extends LinearLayout {
    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.custom_title, this);
        isInEditMode();
        Button titleBack = (Button) findViewById(R.id.title_back);

        Button titleEdit = (Button) findViewById(R.id.title_edit);
        titleBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) getContext()).finish();
            }
        });
        titleEdit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "You clicked Edit button", Toast.LENGTH_SHORT).show();
            }
        });


        TextView tvCustomTitle = findViewById(R.id.title_text);
        // 将属性值设置到控件中
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitleLayout);
        CharSequence text = a.getText(R.styleable.TitleLayout_text1);
        if (text != null && !StringUtils.isEmpty(text.toString().trim())) {
            tvCustomTitle.setText(text);
        }
        a.recycle();
    }
}